<!DOCTYPE html>
<html class="loading" lang="en" data-textdirection="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="author" content="PIXINVENT">
    <title>999后台管理系统</title>
    <script src="app-assets/js/Dream.js"></script>
    <script src="app-assets/js/Http.js"></script>
    <link rel="stylesheet" type="text/css" href="app-assets/css/vendors.css">
    <link rel="stylesheet" type="text/css" href="app-assets/css/vertical-menu.css">
    <link rel="stylesheet" type="text/css" href="app-assets/css/bootstrap-extended.css">
</head>
<body>
<div class="content-wrapper">
    <div class="content-header ">
        <div class="content-header-left mb-2">
            <h3 class="content-header-title mb-0">商品列表</h3>
        </div>
    </div>
    <div class="card">
        <div class="card-content">
            <div class="card-body">
                <div class="searchbox">
                    <form class="form-inline">
                        <div class="form-group">
                            <input type="text" class="form-control" id="productId" placeholder="请填写商品ID">
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" id="productName" placeholder="请填写商品名称">
                        </div>

                        <div class="form-group">
                            <button type="button" class="btn btn-primary" onclick="selects()">查询</button>
                        </div>

                    </form>
                </div>
                <div class="funcbtn clearfix">
                    <div class="pull-left"><a class="btn btn-info" href="编辑商品信息.html"> 添加商品</a></div>
                </div>
                <div class="tablebox1">
                    <table class="table">
                        <thead>
                        <tr>
                            <th>商品图片</th>
                            <th>商品ID</th>
                            <th>商品名称</th>
                            <th>商品类别</th>
                            <th>商品品牌</th>
                            <th>商品售价</th>
                            <th>商品采购价</th>
                            <th>商品描述</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="showT">

                        </tbody>
                    </table>

                    <div class="page clearfix">
                        <div class="pull-left">共 <span id="total"></span> 条记录 第 <span id="pageNum"></span> / <span
                                id="pages"></span> 页
                        </div>
                        <div class="pull-right">

                            <nav aria-label="..." class="pull-left">
                                <ul class="pagination" id="pageTo">

                                </ul>
                            </nav>
                            <div class="pull-left p-l-20"><a class="" href="#" onclick="pageTo()">跳至</a><input
                                    type="text" value="" class="form-control" id="pageNumber">页
                            </div>
                        </div>

                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="app-assets/js/vendors.min.js" type="text/javascript"></script>
<script src="app-assets/js/app-menu.js" type="text/javascript"></script>
<script src="app-assets/js/app.js" type="text/javascript"></script>
<script src="app-assets/js/echarts.min.js" type="text/javascript"></script>

</body>
</html>
<script>
    $(document).ready(function () {
        selectAll($('#productId').val(), $('#productName').val(), 1)
    });

    function selectAll(productId ,productName , pageNum) {
        $.ajax({
            'url': HTTP.getUserInfo+'/products',
            'type': 'get',
            'dataType': 'json',
            'data': {productId ,productName , pageNum},
            'success': selectAllSuccess
        })
    }

    function selects() {
        selectAll($('#productId').val(), $('#productName').val(), 1)
    }

    function selectAllSuccess(data) {
        $('#showT tr').remove()

        data.data.list.forEach(dd => {
            let status = dd.deleteAt == null ? '<span class="text-primary">正常</span>' : '<span class="text-danger">删除</span>'
            $('#showT').append(`<tr>
                            <td><img src="`+HTTP.getUserInfo+`/`+dd.imageUrl+`" width="60"></td>
                            <td>`+ dd.productId +`</td>
                            <td>`+ dd.productName +`</td>
                            <td>`+ dd.categoryName +`</td>
                            <td>`+ dd.brand +`</td>
                            <td>￥`+ dd.price +`</td>
                            <td>￥`+ dd.purchasePrice+`</td>
                            <td>`+ dd.description +`</td>
                            <td>` + status + `</td>
                            <td>
                                <a href="编辑商品信息.html?id=`+ dd.productId +`" class="btn btn-primary btn-td">修改</a>
                                <a href="#" class="btn btn-danger btn-td"  onclick="deleteEm(this)">删除</a>
                            </td>
                        </tr>`)
        })
        $('#total').html(data.data.total)
        $('#pageNum').html(data.data.pageNum)
        $('#pages').html(data.data.pages)
        $('#pageSize').html(data.data.pageSize)
        let upP = ''
        if (data.data.pageNum == 1) {
            upP = 'disabled'
        }
        let neP = ''
        if (data.data.pageNum == data.data.pages) {
            neP = 'disabled'
        }
        let pageTo = ''
        pageTo += `<li class="` + upP + `" onclick="upPage()"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>`
        for (let i = 1; i <= data.data.pages; i++) {
            if (i == data.data.pageNum) {
                pageTo += `<li class="active"><a href="#">${i} <span class="sr-only">(current)</span></a></li>`
            } else {
                pageTo += `<li><a href="#">${i}</a></li>`
            }
        }
        pageTo += `<li class="` + neP + `" onclick="nePage()"><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>`
        $('#pageTo').html(pageTo)

        $('#pageTo a').click(function () {
            let pageNum = $(this).html()
            selectAll($('#productId').val(), $('#productName').val(), pageNum)
        })
    }

    function upPage() {
        let pageNum = $('#pageNum').html()
        pageNum--
        selectAll($('#productId').val(), $('#productName').val(), pageNum)
    }

    function nePage() {
        let pageNum = $('#pageNum').html()
        pageNum++
        selectAll($('#productId').val(), $('#productName').val(), pageNum)
    }

    function pageTo() {
        let pageNum = $('#pageNumber').val()
        selectAll($('#productId').val(), $('#productName').val(), pageNum)
    }


    function deleteEm(e) {
        //获取ID
        let productId = $(e).parent().parent().parent().parent().find('td').eq(1).html()
        $.ajax({
            'url': HTTP.getUserInfo+'/products/' + productId,
            'type': 'delete',
            'dataType': 'json',
            'success': deleteEmSuccess
        })
    }

    function deleteEmSuccess(data) {
        if (data.code == 200) {
            Dreamer.success('删除成功', 1000);
            selectAll($('#productId').val(), $('#productName').val(), 1)
        } else {
            Dreamer.error('删除失败', 1000);
        }
    }
</script>